/**
 * @Author yinyunqi
 * @date 2017年12月28日
 * @Content 借款
 */
package com.yongqi.controller.console;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yongqi.model.Family;
import com.yongqi.model.console.Debt;
import com.yongqi.pub.DateFormat;
import com.yongqi.service.MemberService;
import com.yongqi.service.console.DebtService;

@Controller
@RequestMapping("/debt")
public class DebtController {
	@Autowired
	MemberService memberService;
	@Autowired
	DebtService debtService;
	/**
	 * 跳转至借款主页面 根据本家庭账户获得该家庭账户下拥有的成员账户,赋值于借款人/借出人
	 * 
	 * @return
	 */
	@RequestMapping("/index")
	public String debtIndex(Model model) {
	    	String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		List<String> memberNametList = memberService.selectMemberByFamily(familyAccount);
		model.addAttribute("nameList", memberNametList);
		return "debt/DebtIndex";
	}
	/**
	 * 添加借款记录
	 * 
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	@ResponseBody
	@RequestMapping("/addDebt")
	public String addDebt(HttpServletRequest request) throws SQLException {
	    	String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		Debt debt = new Debt();
		debt.setId(UUID.randomUUID().toString());
		debt.setFamilyaccount(familyAccount);
		debt.setDebttype(request.getParameter("type"));
		debt.setItem(request.getParameter("debtItem"));
		debt.setDate(request.getParameter("debtDate"));
		debt.setAccount(request.getParameter("debtAccount"));
		debt.setMoney(request.getParameter("debtMoney"));
		debt.setPlandate(request.getParameter("debtPlanDate"));
		debt.setBorrowmember(request.getParameter("borrowMember"));
		debt.setLoanmember(request.getParameter("loanMember"));
		debt.setTip(request.getParameter("debtTip"));
		debt.setCreated_time(DateFormat.dateFormate());
		debtService.insertDebt(debt);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", "success");
		return jsonObject.toJSONString();
	}
	/**
     * 借款分页查询
     * @param request
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/query")
    public String queryItems(HttpServletRequest request,@RequestParam int pageNumber ,int pageSize) {
	String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
	Map<String, String> map = new HashMap<String, String>();
	map.put("familyAccount", familyAccount);
		int total = debtService.queryPageItems(map).size();
		PageHelper.startPage(pageNumber,pageSize);
		List<Debt> incomeList = debtService.queryPageItems(map);
		JSONObject object = new JSONObject();
		object.put("rows", incomeList);
		object.put("total", total);
		return object.toJSONString();
    }
    /**
     * 删除预算记录
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public String deleteItems(HttpServletRequest request) {
		String[] ids = request.getParameterValues("ids");
		for(int i=0;i<ids.length;i++) {
			debtService.deleteDebtItems(ids[i]);
		}
		JSONObject object = new JSONObject();
		object.put("result", "success");
		return object.toJSONString();
    }
    /**
     * 修改收入记录
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateItem")
    public String updateItem(HttpServletRequest request) {
	Debt debt = new Debt();
	debt.setId(request.getParameter("id"));
	debt.setDebttype(request.getParameter("type"));
	debt.setItem(request.getParameter("debtItem"));
	debt.setDate(request.getParameter("debtDate"));
	debt.setAccount(request.getParameter("debtAccount"));
	debt.setMoney(request.getParameter("debtMoney"));
	debt.setPlandate(request.getParameter("debtPlanDate"));
	debt.setBorrowmember(request.getParameter("borrowMember"));
	debt.setLoanmember(request.getParameter("loanMember"));
	debt.setTip(request.getParameter("debtTip"));
	debt.setCreated_time(DateFormat.dateFormate());
	debtService.updateDebt(debt);
	JSONObject jsonObject = new JSONObject();
	jsonObject.put("result", "success");
	return jsonObject.toJSONString();
    }
}